<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>药库退货</title>
<script type="text/javascript" src="static/easyui/jquery.min.js"></script>
<script type="text/javascript" src="static/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<link rel="stylesheet" href="static/easyui/themes/icon.css"/>
<link rel="stylesheet" href="static/easyui/themes/macosx/easyui.css"/>
</head>
<body>
<script type="text/javascript">
//时间转换
function comformatDate(date) {
	var day = new Date(date); 
	var Year = 0; 
	var Month = 0; 
	var Day = 0; 
	var CurrentDate = ""; 
	//初始化时间 
	Year= day.getFullYear();//
	Month= day.getMonth()+1; 
	Day = day.getDate(); 
	Hour = day.getHours(); 
	 Minute = day.getMinutes(); 
	 Second = day.getSeconds(); 
	CurrentDate += Year + "-"; 
	if (Month >= 10 ) { 
		CurrentDate += Month + "-"; 
	}else { 
		CurrentDate += "0" + Month + "-"; 
	} 
	if (Day >= 10 ) { 
		CurrentDate += Day ; 
	}else { 
		CurrentDate += "0" + Day ; 
	} 
	if(Hour>=10){
		CurrentDate +=" "+Hour;
	}else{
		CurrentDate +=" "+"0"+Hour;
	}
	if(Minute>=10){
		CurrentDate +=":"+Minute;
	}else{
		CurrentDate +=":"+"0"+Minute;
	}
	if(Second>=10){
		CurrentDate +=":"+Second;
	}else{
		CurrentDate +=":"+"0"+Second;
	}
	return CurrentDate; 
};

//选择药品
$("#drug-list").datagrid({
	 onDblClickRow:function(rowIndex,rowDate){
		 var zhongyao=$("#dept-add-dg").datagrid("getRows");//得到药品表格的所有行
		 if(zhongyao.length==0){//如果他的长度为零,就表示没有一条数据就新增一条
			 rowDate.return_drugs_number=1;//设置一个药品数量默认值
			 
			 $('#dept-add-dg').datagrid('appendRow',rowDate);
				var ypdj=rowDate.drug_selling_price;//得到选中药品的金额
				var ypdjs=parseInt(ypdj); 
				var dqz=$("#return_drugs_price").val();//得到当前的总金额
				var dqzs=parseInt(dqz);
				$("#return_drugs_price").textbox("setValue",ypdjs+dqzs);//重新赋值给药品总金额

		 }else{
			 var a;//ar一个变量作为得到具体哪一行的参数
			 var opin = true;
			 for (var i= 0;i<zhongyao.length;i++){				 
					 if(rowDate.drug_id==zhongyao[i].drug_id){
						 opin = false;
						 a=i;
					 }
				}
			 if(opin){//如果判断表格里面没有相同的药品就新增一行
				 rowDate.return_drugs_number=1;
				 $('#dept-add-dg').datagrid('appendRow',rowDate);
				 	var ypdj=rowDate.drug_selling_price;//得到选中药品的金额
				 	var ypdjs=parseInt(ypdj); 
					var dqz=$("#return_drugs_price").val();//得到当前的总金额
					var dqzs=parseInt(dqz);
					$("#return_drugs_price").textbox("setValue",ypdjs+dqzs);//重新赋值给药品总金额
				 
			 }else{//判断表格里面有相同的药品就数量加一
				 rowDate.return_drugs_number=parseInt(rowDate.return_drugs_number)+parseInt(1);
				 $('#dept-add-dg').datagrid('updateRow',{index:a,row:{rowDate}}); 
			 }
			 var sum = 0;//定义一个值来存储金额
			 for (var i = 0; i<zhongyao.length; i++) {//循环右边药单所有行取出药品数量和单价相乘
				var row = zhongyao[i];
				sum = sum + parseInt(row.drug_selling_price)*parseInt(row.return_drugs_number);
			 } 
			 $("#return_drugs_price").textbox("setValue",sum);//重新赋值给药品总金额
		 }
	 }
});


	
	//选择药品表格
	$(function(){	
		$('#drug-list').datagrid({
			url:'yaopinbiaoge',
			pagination:true,
		    pageNumber:1,
		    pageSize:5,
		    pageList:[5,10],
			columns:[[
				{field:'inventory_yk_id',title:'库存编号',width:"140px",align:'center'},
				//{field:'drug_id',title:'药品编号',width:"140px",align:'center'},
				{field:'drug_name',title:'药品名称',width:"140px",align:'center'},
				{field:'drug_specifications',title:'药品规格',width:"140px",align:'center'},
				{field:'drug_pc',title:'批次',width:"140px",align:'center'},
				{field:'drug_selling_price',title:'销售价',width:"140px",align:'center'},
				{field:'drug_types_name',title:'药品类型',width:"140px",align:'center'},
				{field:'inventory_number',title:'数量',width:"140px",align:'center'}
			]]		  		
		});	
	}); 	
	
	//第一个选项卡初始化表格
	$(function(){	
		//退货表格
		$('#tuihuobiaoge').datagrid({
		    url:'thbg',
		    columns:[[
				{field:'return_drugs_id',title:'退货编号',width:"160px",align:'center'},
				{field:'return_drugs_number',title:'退货数量',width:"160px",align:'center'}, 
				{field:'return_drugs_price',title:'退货金额',width:"160px",align:'center'},
				{field:'return_drugs_time',title:'退货时间',width:"160px",align:'center',formatter:comformatDate},
				{field:'return_drugs_people',title:'退货人',width:"160px",align:'center'},
				{field:'return_yuanyin',title:'退货原因',width:"160px",align:'center'},
				{field:'cz',title:'操作',width:160,align:'center',
					formatter:function(value,row,index){
						return "<input type='button' style='background:#E4F1FB;color:#2779AA;border:none' onclick='shenhetuihuo("+row.return_drugs_id+")' value='查看详情'/>";
					}	
				}
		    ]],
		    pagination:true,
		    pageNumber:1,
		    pageSize:5,
		    pageList:[5,10,15]
		});			
	});	
	//新增显示选择药品界面
	$("#tuihuoanniu").click(function(){
		$("#xuanze").window("open");
	});
	
	//点击保存按钮
	$("#baocun").click(function(){
		$("<form method='post'>").appendTo("body").form({
			url:'tuihuo-save',
			onSubmit:function(param){
				//退货时间
				//param["tuihuo.returnDrugsTime"] = new Date();
				//退货人
				param["tuihuo.returnDrugsPeople"] = $("#return_drugs_people").val();
				//退货金额
				param["tuihuo.returnDrugsPrice"] = $("#return_drugs_price").val();
				//退货原因
				param["tuihuo.returnYuanyin"] = $("#return_yuanyin").val();
				var sum=0;
				//获取表格中的采购的详情记录
				var rows = $("#dept-add-dg").datagrid("getRows");
				//console.log(rows);
				for (var i = 0; i < rows.length; i++) {
					//库存编号
					param["tuihuoxq["+i+"].InventoryYk.inventoryYkId"] = rows[i]["inventory_yk_id"];
					//退货数量
					param["tuihuoxq["+i+"].returnDenumber"] = rows[i]["return_drugs_number"];
					//采购价格
					param["tuihuoxq["+i+"].drugSellingPrice"] = rows[i]["drug_selling_price"];
					var row=rows[i];
					sum=sum+parseInt(row.return_drugs_number);					
				} 
				//退货数量				
				param["tuihuo.returnDrugsNumber"] =sum;
				//alert("数量"+sum);
			},
			success:function(d){				
				$.messager.show({
					title:'提 示',
					msg:'退货新增成功！！！！',
					height:200,
					timeout:1000,
					showType:'slide'
				});
				//退货时间
				$("#return_drugs_time").textbox('setValue',null);
				//退货金额
				$("#return_drugs_price").textbox('setValue',null);
				//退货原因
				$("#return_yuanyin").textbox('setValue',null);
				//退货人
				$("#return_drugs_people").textbox('setValue',null);
				//刷新
				$("#tuihuobiaoge").datagrid("reload");
			}
		}).submit();
	});	
	//进行审核查看详情
	function shenhetuihuo(a){
		$("#shenhejiemian").window("open");//打开采购详情界面
		var row=$('#tuihuobiaoge').datagrid('getSelected');
		var id2 = row["return_drugs_id"];//id
		//alert(id2);
		$("#tongguo").val(id2); 
		//============审核详情表格
		$('#shenhexiangqing').datagrid({
		    url:'tuihxq',
		    queryParams: {
		    	return_drugs_id:id2
		    },
		    columns:[[
		    	{field:'',checkbox:true,width:"130px",align:'center'},
				{field:'return_details_id',title:'详情编号',width:"130px",align:'center'},    
				{field:'inventory_yk_id',title:'库存编号',width:"130px",align:'center'},
				{field:'drug_pc',title:'批次',width:"130px",align:'center'},
				{field:'return_denumber',title:'数量',width:"130px",align:'center',editor:'numberspinner'},
				{field:'drug_name',title:'药品名称',width:"130px",align:'center'}
		    ]],
		    pagination:true,
		    pageNumber:1,
		    pageSize:5,
		    pageList:[5,10,15]			       
		});	
	}
	 //审核通过按钮
	 $("#shenhetongguo").click(function(){			
			//退货修改数量=====减少库存
			var hh = $('#shenhexiangqing').datagrid("getRows");				
			for(var i=0; i< hh.length;i++){
				var inventory_yk_id = hh[i]["inventory_yk_id"];
				var bb = hh[i]["return_denumber"];				 
				//alert(inventory_yk_id+"sssss");						
				$.ajax({
					url : 'jianshao',
					type : 'post',
					data : {
						"inventory_yk_id":inventory_yk_id,
						"bb":bb,
					},
					success : function(data){
						$.messager.show({
							title:'提 示',
							msg:'成功！！！！',
							height:200,
							timeout:1000,
							showType:'slide'
						});
					}
				});						
			}
	 	//修改状态
		/* var aa=$('#tongguo').val();
		$.ajax({
			url:'xgztid',
			type:'post',
			data:{
				'returnDrugsId':aa,
			},
			success:function(d){
				if(parseInt(d)==1){
					$("#tuihuobiaoge").datagrid("reload");
				}else{
					alert("退货失败");
				}
			}
		}) */
		$("#shenhejiemian").window("close");
	}); 

	//文本框回车事件搜索
		$('#exchangeSear').searchbox({
			searcher:function(value,name){
				$('#tuihuobiaoge').datagrid({
					url:'thbg?return_drugs_id='+value,	
				});				  
			}
		});
	 
	 
	//生产批次点击---修改批次
		$.extend($.fn.datagrid.methods, {
			editCell: function(jq,param){
				return jq.each(function(){
					var opts = $(this).datagrid('options');
					var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor1 = col.editor;
						if (fields[i] != param.field){
							col.editor = null;
						}
					}
					$(this).datagrid('beginEdit', param.index);
					for(var i=0; i<fields.length; i++){
						var col = $(this).datagrid('getColumnOption', fields[i]);
						col.editor = col.editor1;
					}
				});
			}
		});
		var editIndex = undefined;
		function endEditing(){
			if (editIndex == undefined){return true}
			if ($('#shenhexiangqing').datagrid('validateRow', editIndex)){
				$('#shenhexiangqing').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
		function onClickCell(index, field){ //下面表格里要有这个点击
			if (endEditing()){
				$('#shenhexiangqing').datagrid('selectRow', index)
						.datagrid('editCell', {index:index,field:field});
				editIndex = index;
			}
		} 
	 
	
	
	//表格中的文本框
	function formatAction(value,row,index){
		if (row.editing){
			var s = "<a href='#' onclick='saverow("+index+")'>保存</a> ";
			var c = '<a href="#" onclick="cancelrow(this)">返回</a>';
			return s+c;
		}  else {
			var e = '<a href="#" onclick="editrow(this)">修改</a> ';
			var d = '<a href="#" onclick="deleterow(this)">删除</a>'; 
			return e+d;
		} 
	}			
	$.extend($.fn.datagrid.defaults.editors, {
		numberspinner: {
			init: function(container, options){
				var input = $('<input type="text">').appendTo(container);
				return input.numberspinner(options);
			},
			destroy: function(target){
				$(target).numberspinner('destroy');
			},
			getValue: function(target){
				return $(target).numberspinner('getValue');
			},
			setValue: function(target, value){
				$(target).numberspinner('setValue',value);
			},
			resize: function(target, width){
				$(target).numberspinner('resize',width);
			}
		}
	});
	$(function(){
		$('#dept-add-dg').datagrid({
			onBeforeEdit:function(index,row){
				$(this).datagrid('updateRow', {index:index,row:{editing:true}})
			},
			onAfterEdit:function(index,row){
				$(this).datagrid('updateRow', {index:index,row:{editing:false}})
			},
			onCancelEdit:function(index,row){
				$(this).datagrid('updateRow', {index:index,row:{editing:false}})
			}
		});
	});
	function getRowIndex(target){
		var tr = $(target).closest('tr.datagrid-row');
		return parseInt(tr.attr('datagrid-row-index'));
	}
	function editrow(target){
		$('#dept-add-dg').datagrid('beginEdit', getRowIndex(target));
	}
	//删除事件
	function deleterow(target){
		$.messager.confirm('Confirm','确定删除?',function(r){
			if (r){
				$('#dept-add-dg').datagrid('deleteRow', getRowIndex(target));
				var rows = $('#dept-add-dg').datagrid("getRows");
				
				var bb = 0;
				for (var i = 0; i < rows.length; i++) {
					var row =rows[i];
					bb = bb + parseInt(row.drug_selling_price)*parseInt(row.return_drugs_number);
				}
				$("#return_drugs_price").textbox('setValue',bb);
			}
		});
	}
	//保存事件
	function saverow(target){
		var rows = $('#dept-add-dg').datagrid("getRows");
		$('#dept-add-dg').datagrid('endEdit', target);
	
		var bb = 0;
		for (var i = 0; i < rows.length; i++) {
			if(rows[i]["return_drugs_number"]<=0){
				rows[i]["return_drugs_number"]=1;
				$('#dept-add-dg').datagrid('refreshRow',i);
				 for (var i = 0; i < rows.length; i++) {
					 var row = rows[i];
					sum = sum + parseInt(row.drug_selling_price)*parseInt(row.return_drugs_number);
				}
				 $("#return_drugs_price").textbox('setValue',sum);//重新赋值给药品总金 
				 break;
			}else if(rows[i]["return_drugs_number"]==''){
				rows[i]["return_drugs_number"]=1;
				$('#dept-add-dg').datagrid('refreshRow',i);
				 for (var i = 0; i < rows.length; i++) {
					 var row = rows[i];
					sum = sum + parseInt(row.drug_selling_price)*parseInt(row.return_drugs_number);
				}
				 $("#return_drugs_price").textbox('setValue',sum);//重新赋值给药品总金 
				 break;
			}else if(!(/^[1-9]\d*$/).test(rows[i]["return_drugs_number"])){
				
				rows[i]["return_drugs_number"]=1;
				$('#dept-add-dg').datagrid('refreshRow',i);
			 for (var i = 0; i < rows.length; i++) {
				 var row = rows[i];
				sum = sum + parseInt(row.drug_selling_price)*parseInt(row.return_drugs_number);
				}
			 $("#return_drugs_price").textbox('setValue',sum);//重新赋值给药品总金 
			 break;
			}else{
				var row =rows[i];
				bb = bb + parseInt(row.drug_selling_price)*parseInt(row.return_drugs_number);
			}
		}
		$("#return_drugs_price").textbox('setValue',bb);
	}
	function cancelrow(target){
		$('#dept-add-dg').datagrid('cancelEdit', getRowIndex(target));
	}	
</script>
<!-- =======隐藏域======= -->
<input type="hidden" type="text" id="tongguo">
		    <div>
				<div class="easyui-layout" style="padding:20px;">
					 	<!-- <input id="return_drugs_time" class="easyui-datebox" data-options="label:'退货时间：',width:260,labelAlign:'right'"> -->
					 	<input id="return_drugs_price" class="easyui-textbox" readonly data-options="label:'退货金额：',value:0,width:260,labelAlign:'right'">			 	
						<input id="return_yuanyin" class="easyui-textbox" data-options="label:'退货原因：',width:260,labelAlign:'right'">
						<input id="return_drugs_people" value="${user.userName}" class="easyui-textbox" data-options="label:'退货人：',width:260,labelAlign:'right'">
						<a id="tuihuoanniu" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" style="margin-left:450px;margin-top:20px;">添加药品</a>
						<a id="baocun" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true" style="margin-top:20px;">保存</a>					
				</div>
				<!-- 添加的药品表格 -->
				<div style="height:200px;">
					<table id="dept-add-dg" class="easyui-datagrid" data-options="fit:true,fitColumns:true">    	
				    	<thead>   
						   <tr>   
						   	  <th data-options="field:'inventory_yk_id',width:100">库存编号</th>
						   	  <th data-options="field:'drug_id',width:100">药品编号</th>   
							  <th data-options="field:'drug_name',width:100">药品名称</th>   
							  <th data-options="field:'drug_selling_price',width:100">药品价格</th>   
							  <th data-options="field:'drug_specifications',width:100">规格</th>   
							  <th data-options="field:'return_drugs_number',width:100,editor:'text'">退货数量</th>
						   	  <th data-options="field:'caozuo',width:100,formatter:formatAction">操作</th>
						   </tr>   
						</thead>
					</table>
				</div>		 
				<div style="padding:10px;">
					<input id="exchangeSear" class="easyui-searchbox" style="width:280px;height:30px;"
						data-options="label:'退货编号：',prompt:'请输入退货编号',labelAlign:'right'">							
				</div>		
				<!-- 退货药品列表 -->
				<table id="tuihuobiaoge" class="easyui-datagrid" data-options="fitColumns:true" style="padding:15px;height:300px;"></table>
			</div>	

			<!-- 选择药品界面 -->
			<div id="xuanze" class="easyui-window" title="选择退货药品" style="width:1000px;height:335px" data-options="iconcls:'icon-add',closed:true">
				<table id="drug-list" class="easyui-datagrid" data-options="fitcolumns:true" style="padding:15px;height:290px;"></table>	 		
			</div>
			
			<!-- 进行审核界面 -->
			<div id="shenhejiemian" class="easyui-window" title="审核详情" style="width:700px;height:400px" data-options="iconCls:'icon-add',closed:true">
				<table id="shenhexiangqing" class="easyui-datagrid" data-options="fitColumns:true" style="padding:15px;height:300px;"></table>
				<a id="shenhetongguo" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true" style="margin-left:506px;margin-top:20px;">确认退货</a>
			</div>				 		
</body>
</html> 